#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>  //for exit()
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include "net.h"
#include "mytype.h"
#include "debug.h"
#include <sys/types.h>

int open_client(char *ipaddr)
{
	int iRet,clientfd,n;
	FILE fp;
	char buffer[MAXLINE];
	struct sockaddr_in server;
	bzero(&server,sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(9999);
	inet_pton(AF_INET,ipaddr,&server.sin_addr);
	if((clientfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("socket creat error");
		return -1;
	}
	if((iRet = connect(clientfd,(struct sockaddr*) &server,sizeof(server))) == -1)
	{
		perror("connect error");
		return -1;
	}
	
	bzero(buffer,0);
		while(fgets(buffer,sizeof(buffer),stdin) != NULL)
		{
			printf("we send: %s",buffer);
			n=write(clientfd,buffer,strlen(buffer)+1);
			bzero(buffer,0);
			if((n=read(clientfd,buffer,sizeof(buffer)))!=0)
            {   
                if(strncmp(buffer,"exit",strlen("exit"))==0)
                {
                    exit(0);
                }
                printf("we get from server: %s",buffer);
            }
			bzero(buffer,0);
		}
	return 0;
}

int main()
{
	open_client("127.0.0.1");
}


